package com.pingan.framework.cat.config.internals;

import org.unidal.cat.config.internals.DefaultClientEnvironmentSettings;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

public enum  Environment {

    PRO,
    UAT,
    FWS,
    FAT,
    DEV,
    UNKNOWN;
    private static Environment current = null;

    public static Environment getCurrent(){

        if(current == null) {

            synchronized (Environment.class) {

                if(current == null) {
                    Properties properties = new Properties();
                    try {
                        String resource = "/META-INF/cat/domain.properties";
                        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);

                        if (in == null) {
                            in = DefaultClientEnvironmentSettings.class.getResourceAsStream(resource);
                        }
                        properties.load(in);



                        String[] envs = new String[]{"DEV","FAT","FWS","UAT","PRO"};
                        for (String n : envs) {

                            final AtomicBoolean canReach = new AtomicBoolean(false);
                            final String val = properties.getProperty(n);
                            if(val == null){
                                continue;
                            }

                            Thread t1 = new Thread() {
                                public void run() {
                                    canReach.set(canReach(val));
                                }
                            };
                            t1.start();
                            t1.join(150);
                            if (canReach.get()) {
                                String s = n.toUpperCase();

                                if (s.equals("PRO")) {
                                    return PRO;
                                } else if (s.equals("UAT")) {
                                    return UAT;
                                } else if (s.equals("FWS")) {
                                    return FWS;
                                } else if (s.equals("FAT")) {
                                    return FAT;
                                } else if (s.equals("DEV")) {
                                    return DEV;
                                }
                            }

                        }
                    } catch (Throwable e) {
                        e.printStackTrace();

                    }
                }
            }
        }
        return UNKNOWN;
    }

    public static void reset(){
        current = null;
    }
    private static boolean canReach(final String domain){
        String[] infos = domain.split(":");

        String host = infos[0];
        int port = infos.length>1?Integer.parseInt(infos[1]):80;

        try {
            if (host.startsWith("http")) {
                host = new URL(host).getHost();
            }


            InetSocketAddress socketAddress = new InetSocketAddress(host, port);
            Socket soc = new Socket();

            try {
                soc.connect(socketAddress, 100);
                return true;
            } finally {
                soc.close();

            }
        }catch (Throwable e) {
            e.printStackTrace();
        }

        return false;
    }


}
